package 真题;
/**
 * @author： li
 * @date： 2022-04-06 08:22
 * @version 1.0
 */
import java.util.PriorityQueue;
import java.util.Scanner;
public class 哈夫曼树_优先队列 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        PriorityQueue<Integer> que = new PriorityQueue<>();
        int n =sc.nextInt();
        while(n>0){
            int x =sc.nextInt();
            que.offer(x);
            n--;
        }
        int ans =0;
        while(que.size()>1){
            int x1 =que.poll();
            int x2 =que.poll();
            int sum =x1+x2;
            ans+=sum;
            que.offer(sum);
        }
        System.out.println(ans);
    }
}
